package com.freshdirect.src.controller;

import static org.apache.commons.lang.StringUtils.isNumeric;
import com.freshdirect.src.model.ShoppingCart;
import com.freshdirect.src.model.User;
import com.freshdirect.src.service.OrderService;
import com.freshdirect.src.service.ShoppingCartService;
import com.freshdirect.src.service.UserService;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class OrderController {
        
        @RequestMapping(value="/order/checkout", method = RequestMethod.GET)
         public String checkout(Model model,HttpSession session) {
            if(session.getAttribute("userName")!=null){
                ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                ShoppingCartService scs= new ShoppingCartService();
                cart = scs.checkAvailability(cart);
                session.setAttribute("cart", cart);
                
                UserService userService= new UserService();
                User user = userService.ViewUserdetais((String) session.getAttribute("userName"));
                model.addAttribute("user",user);
     
               return "checkout";
            }
            else{
                return "redirect:/signup";
            }
        }
         
        
        @RequestMapping(value="/order/checkout", method = RequestMethod.POST)
         public String checkoutfinal(@RequestParam("cardnumber") String cardnumber, @RequestParam("holdername") String holdername, @RequestParam("cardtype") int cardtype, Model model,HttpSession session) {
             if(session.getAttribute("userName")!=null){
                if(holdername.equals("")|| cardnumber.equals("")|| cardtype == 0 || !isNumeric(cardnumber)){
                    ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                    session.setAttribute("cart", cart);
                    UserService userService= new UserService();
                    User user = userService.ViewUserdetais((String) session.getAttribute("userName"));
                    model.addAttribute("user",user);
                    model.addAttribute("message","Please fill Credit Card Information");
                    return "checkout";
                }
                else{
                    ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                    ShoppingCartService scs= new ShoppingCartService();
                    cart = scs.checkAvailability(cart);
                    ///
                    UserService us = new UserService();
                    us.updateCreditCard((Integer) session.getAttribute("userID"),cardnumber, holdername,cardtype);
                    ///
                    OrderService os = new OrderService();
                    int flag = os.createOrder(cart, (Integer) session.getAttribute("userID"));
                    ///
                    if(flag != 0){
                        session.setAttribute("cart", cart);
                        UserService userService= new UserService();
                        User user = userService.ViewUserdetais((String) session.getAttribute("userName"));
                        model.addAttribute("user",user);
                        return "checkout";
                    }
                    else{
                        ShoppingCart cart2 =  new ShoppingCart();
                        session.setAttribute("cart", cart2);
                        model.addAttribute("cart", cart2);
                        return "home";
                    }
                }
            }
            else{
                return "redirect:/signup";
            }
         }
         
}
